home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / FMT.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  5KB  |  136 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    Program:    fmt.c          14 August 1990                       */
  3. /*    Author:     Andrew H. Derbyshire                                */
  4. /*                108 Decatur St, Apt 9                               */
  5. /*                Arlington, MA 02174                                 */
  6. /*    Function:   Format lines into user specified width rows         */
  7. /*    Arguments:  width, input file, output file.                     */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14.  
  15. #include "lib.h"
  16. #include "timestmp.h"
  17.  
  18. /*--------------------------------------------------------------------*/
  19. /*    m a i n                                                         */
  20. /*                                                                    */
  21. /*    main program                                                    */
  22. /*--------------------------------------------------------------------*/
  23.  
  24.  void main( int argc, char *argv[] )
  25.  {
  26.    int width = 0;          /* Width of current line                  */
  27.    int maxwidth = 72;      /* Max width of line allowed              */
  28.    char buf[BUFSIZ];       /* Our I/O buffer                         */
  29.    int punct = 0;          /* Last character in last word was punct  */
  30.    int argx = 1;           /* Current argument being processed       */
  31.    FILE *input;
  32.    FILE *output;
  33.  
  34. /*--------------------------------------------------------------------*/
  35. /*                        Announce our version                        */
  36. /*--------------------------------------------------------------------*/
  37.  
  38.    banner( argv );
  39.  
  40. /*--------------------------------------------------------------------*/
  41. /*                            Handle help                             */
  42. /*--------------------------------------------------------------------*/
  43.  
  44.    if (( argc > 1 ) && equal(argv[1],"-?"))
  45.    {
  46.       printf("Usage:\tfmt\t[-#] infile outfile\n");
  47.       exit(1);
  48.    }
  49.  
  50. /*--------------------------------------------------------------------*/
  51. /*       Get the line width if the user specified it                  */
  52. /*--------------------------------------------------------------------*/
  53.  
  54.     if ((argx < argc) && (*argv[argx] == '-'))
  55.       maxwidth = atoi( argv[argx++] );
  56.  
  57. /*--------------------------------------------------------------------*/
  58. /*                  Get the input file name, if any                   */
  59. /*--------------------------------------------------------------------*/
  60.  
  61.     if (argx == argc)
  62.       input = stdin;
  63.     else {
  64.       input = fopen(argv[argx++],"r");
  65.       if (input == NULL)
  66.       {
  67.          perror(argv[--argx]);
  68.          exit (100);
  69.       }
  70.     }
  71.  
  72. /*--------------------------------------------------------------------*/
  73. /*                  Get the output file name, if any                  */
  74. /*--------------------------------------------------------------------*/
  75.  
  76.     if (argx == argc )
  77.       output = stdout;
  78.     else {
  79.       output = fopen(argv[argx++],"w");
  80.       if (output == NULL)
  81.       {
  82.          perror(argv[--argx]);
  83.          exit( 200 );
  84.       }
  85.     }
  86.  
  87. /*--------------------------------------------------------------------*/
  88. /*                          Process the file                          */
  89. /*--------------------------------------------------------------------*/
  90.  
  91.     while( fgets(buf, BUFSIZ, input) != NULL )
  92.     {
  93.       char *token = strtok(buf, " \t\n");
  94.       if (token == NULL )
  95.       {
  96.          fputc('\n',output);
  97.          width = punct = 0;
  98.       }
  99.       else while(token != NULL)
  100.       {
  101.          register size_t toklen = strlen(token);
  102.          width = toklen + width + 1 + punct;
  103.          if (width > max(maxwidth, toklen + 1))
  104.          {
  105.             fputc('\n',output);
  106.             width = toklen;
  107.             punct = 0;
  108.          }
  109.          else {
  110.             if (width > (toklen + 1))
  111.             {
  112.                fputc(' ',output);
  113.                if (punct)
  114.                   fputc(' ',output);
  115.             }
  116.             else
  117.                width = toklen;
  118.             punct = ispunct( token[toklen - 1] ) ? 1 : 0;
  119.          } /* else */
  120.          fputs(token, output);
  121.          token = strtok(NULL, " \t\n");
  122.       } /* else while */
  123.    } /* while */
  124.  
  125.    if (ferror(input))
  126.    {
  127.       perror(argv[1]);
  128.       clearerr(input);
  129.    }
  130.  
  131.    fclose(input);
  132.    fclose(output);
  133.  
  134.    exit (0);
  135.  } /* main */
  136.